package com.neu.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.neu.entity.Fmeditem;
import com.neu.listener.FmeditemListener;
import com.neu.mapper.FmeditemMapper;
import com.neu.service.FmeditemService;



@Service
public class FmeditemServiceImpl implements FmeditemService{
	@Autowired
	private FmeditemMapper fmeditemMapper;
	@Override
	public boolean addFmeditem(Fmeditem fmeditem) {
		// TODO Auto-generated method stub
		int save = fmeditemMapper.addFmeditem(fmeditem);
		boolean flag = false;
		if(save>0) {
			flag = true;
		}
		return flag;
	}
	@Override
	public boolean deleteFmeditem(int id) {
		// TODO Auto-generated method stub
		int del = fmeditemMapper.deleteFmeditem(id);
		boolean flag = false;
		if(del>0) {
			flag = true;
		}
		return flag;
	}
	@Override
	public boolean delFmeditemByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		int dels =  fmeditemMapper.delFmeditemByIds(ids);
		return dels==ids.length;	
	}
	@Override
	public boolean updateFmeditem(Fmeditem fmeditem) {
		// TODO Auto-generated method stub
		int upd = fmeditemMapper.updateFmeditem(fmeditem);
		boolean flag = false;
		if(upd>0) {
			flag = true;
		}
		return flag;
	}
	@Override
	public List<Fmeditem> pageFmeditem(int page, int limit, String itemName, int recordType) {
		// TODO Auto-generated method stub
		return fmeditemMapper.pageFmeditem(page,limit,itemName,recordType);
	}
	@Override
	public int pageFmeditemTotal(String itemName, int recordType) {
		// TODO Auto-generated method stub
		return fmeditemMapper.pageFmeditemTotal(itemName,recordType);
	}
	@Override
	public void exportExcel(HttpServletResponse response) {
		// 导出execl
		 try {
	    		response.setContentType("application/vnd.ms-excel");
	            response.setCharacterEncoding("utf-8");
				String fileName = URLEncoder.encode("非药品收费项目", "UTF-8");
				response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
				 
				 try {
					OutputStream outputStream = response.getOutputStream();
					 EasyExcel.write(outputStream, Fmeditem.class).sheet("非药品收费项目").doWrite(fmeditemMapper.findAll());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	@Override
	public void importExcel(MultipartFile file) {
		// 导入excel
		try {
			InputStream inputStream = file.getInputStream();
			EasyExcel.read(inputStream, Fmeditem.class, new FmeditemListener(fmeditemMapper)).sheet("非药品收费项目").doRead();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
